function get_price(){	
    $.getJSON("http://sms.jackwoo.com/do_get_price.php?format=json&jsoncallback=?",
        function(data){
            if(data.result == 0){
                $("#price").append("You need to connect to the internet to purchase credits");	
            }else $("#price").append(data.result);
        }
        );
}

function loadSMS(target){
    
    var url = 'sms/loadSMS';	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        target:target
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}
	
function textCounter(field,  maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    } else {
        $("#chars_left").html(maxlimit - field.value.length);
    }
}
				
function sendSMS()
{    
    var object = $('#object').val();
    var checked = false;
    var option = $('#the_group').val();
    var count = 0;
    $('.items').each(function(){        
        checked = $(this).attr('checked');
        if(checked == true){
            count++;
        }
    })    
    $('#errorObject').html('');
    $('#errorGroup').html('');
    if(count == 0){
        $('#errorObject').html(jQuery.validator.messages['required']);
        $('#errorObject').addClass('error');        
    }else{     
        $('#myform').submit();       
    }
}

function sendSMSByNumber()
{            
    $('#errorObject').html('');    
    if(errorValue.length > 0){
        $('#errorObject').html(jQuery.validator.messages['number']);
        $('#errorObject').addClass('error');        
        return
    }else{
        $('#myform').submit(); 
    }
     
}
var errorValue = Array();
function checkNumber(value){    
    var check = true;    
    var dot = '';
    for(i=0;i<value.length;i++){        
        if(value.indexOf('.') > 0){
            return false;
        }
    }
    var arr = value.split(",");    
    for (i = 0; i < arr.length; i++) 
    { 
        var a = arr[i].length;
        if ((isNaN(arr[i]) || a < 5 || a > 15) && (i != arr.length - 1 || a != 0))
            {
                check = false;
                break;
            }
    }    
    //apply appropriate colour based on value
    if(!check)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function loadManageGroups(){
    
    var url = 'sms/loadManageGroups';
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
       
        }, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
}

function addGroup()
{
    var url = 'sms/addGroup';	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        }, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
}

function deleteGroup(group_id)
{
    var url = 'sms/deleteGroup';
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        group_id:group_id
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function updateManageGroups(){
    if(errorValue.length > 0){            
        $('#errorObject').html(jQuery.validator.messages['number']);
        $('#errorObject').addClass('error');  
    }else{
        $('#myform').submit();
    }
}


function loadPurchaseCredits(){
    
    var url = 'sms/loadPurchaseCredits';
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        
        }, function(data) {            
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
}

function doPurchaseCredits(){
    
    var url = 'sms/doPurchaseCredits';
    var price = $("#price").val();
    var comnum = $("#comnum").val();
    var username = $("#username").val();
	
    var i = $("#i").val();
    //var url = 'sms.jackwoo.com/do_purchase.php';
    // $.getJSON("http://sms.jackwoo.com/do_purchase.php?price="+price+"&format=json&comnum="+comnum+"&username="+username+"&jsoncallback=?",
    // function(data){
    // if(data.result == 1){
    // var quantity = data.quantity;
    // var pricenow = data.price;
    // var transaction_id = data.transaction_id;
    // var credits = data.credits;
    // $.post("do_purchase.php", {pricenow:pricenow, quantity:quantity, username:username, transaction_id:transaction_id, i:i}, function(data) {
    // if(data != 0) {
    // if(i == 1) $("#no_msg").remove();
    // $("#mytable").append(data).slideDown();							
    // $("#credit").html(credits).fadeIn();
    // }
    // }, "html");
    // }else alert("try again OR refresh and try again");	
    // }
    // );
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        price:price, 
        comnum:comnum,
        username:username
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}